#!/bin/bash
thisDistro="Unknown"
function judgeOS() {
    # Check if lsb_release command is available
    if command -v lsb_release > /dev/null 2>&1; then
        # Get distribution information using lsb_release
        distro=$(lsb_release -si)
    else
        # If lsb_release command is not available, fallback to /etc/os-release
        if [ -r /etc/os-release ]; then
            # Read the contents of /etc/os-release
            . /etc/os-release
            # Get distribution information from /etc/os-release
            if [ -n "$NAME" ]; then
                distro=$NAME
            elif [ -n "$ID" ]; then
                distro=$ID
            fi
        fi
    fi
    # Convert the distribution name to lowercase
    distro=$(echo "$distro" | tr '[:upper:]' '[:lower:]')
    # Check the distribution and print the result
    case $distro in
        ubuntu)
            echo "Distribution: Ubuntu"
            thisDistro="ubuntu"
            ;;
        debian)
            echo "Distribution: Debian"
            thisDistro="debian"
            ;;
        centos)
            echo "Distribution: CentOS"
            thisDistro="centos"
            ;;
        fedora)
            echo "Distribution: Fedora"
            thisDistro="fedora"
            ;;
        opensuse)
            echo "Distribution: openSUSE"
            thisDistro="opensuse"
            ;;
        arch)
            echo "Distribution: Arch"
            thisDistro="arch"
            ;;
        manjaro)
            echo "Distribution: Manjaro"
            thisDistro="manjaro"
            ;;
        chromeos)
            echo "Distribution: ChromeOS"
            thisDistro="chromeos"
            ;;
        *)
            echo "Distribution: Unknown"
            ;;
    esac
}
function main(){
    case $1 in 
        -j|--judgeOS)
            judgeOS
            ;;
        -c|--changeSourceFile)
            judgeOS
            echo "Changing source file for $thisDistro "
            case $distro in
                ubuntu)
                    architecture=$(bash ubuntu/judgeUbuntu.sh)
                        if [[ $architecture = "x86" || $architecture = "x86_64" || $architecture = "i386" ]]; then
                            echo "Architecture: $architecture"
                            bash ubuntu/ubuntu/changeSourceFile.sh
                        else
                            echo "Architecture: $architecture"
                            bash ubuntu/ports/changeSourceFile.sh
                        fi
                    ;;
                debian)

                    ;;
                centos)

                    ;;
                fedora)

                    ;;
                opensuse)

                    ;;
                arch)

                    ;;
                manjaro)

                    ;;
                chromeos)

                    ;;
                *)
                    echo "Distribution: Unknown"
                    exit 1
                    ;;
            esac
            ;;
        -h|--help|*)
            echo "Usage: $0 [-h|--help]               Show this help message"
            echo "Usage: $0 [-j|--judgeOS]            Show the distribution name"
            echo "Usage: $0 [-c|--changeSourceFile]   Change the source file for the distribution"
            exit 1
            ;;
    esac
}
main $@